Ext.define('Jason.view.protal.authentication.AuthenticationController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.authentication',
    init : function(){
        session.set('userKeyPlateLogin',true);
        session.set('SINGLEONLINE',true);
        session.set('enableIM',true);
    },
    onFaceBookLogin : function(button, e) {
        EUtil.showMsg({message:I18N.StayTunedForUpdates});
    },
    onLoginButton: function(button, e, eOpts) {
        var self = this,
            authdialog =  button.up('authdialog');
        if(!authdialog.getForm().isValid()){
        	return;
        }
        EUtil.RS({
            url: reqUrl.sys.login,
            params: {
                loginName: authdialog.down('textfield[name=userid]').getValue(),
                password : authdialog.down('textfield[name=password]').getValue(),
                dynamciPassCode : authdialog.down('textfield[name=dynamicCode]').getValue()
            },
            msg:true,
            callback:function(result){
                if(result.success){
                    session.set('loginState',true);
                    if(session.get('token')){
                    	session.set('token',result.root.token);
                    }
                    if(session.get('username')){
                    	session.set('username',result.root.username);
                    }
                    if(session.get('realname')){
                    	session.set('realname',result.root.realname);
                    }
                    if(session.get('dept')){
                    	session.set('dept',result.root.dept);
                    }
                    if(session.get('post')){
                    	session.set('post',result.root.post);
                    }
                    if(session.get('theme')){
                    	session.set('theme',result.root.theme);
                    }
                    session.set('access_token',result.root.token);
                    Ext.util.History.setHash("");
                    window.location.reload();
                }else{
                    EUtil.toast(result.msg_info,2);
                }
            }
        });
    },

    onLoginAsButton: function(button, e, eOpts) {
        this.redirectTo("protal.authentication.Login");
    },

    onNewAccount:  function(button, e, eOpts) {
        this.redirectTo("protal.authentication.Register");
    },

    onSignupClick:  function(button, e, eOpts) {
        this.redirectTo("protal.dashboard");
    },

    onResetClick:  function(button, e, eOpts) {
        this.redirectTo("protal.dashboard");
    },
    onPersistChange : function(this_, newValue, oldValue, eOpts ){
        var self = this,
            check = this_.getValue(),
            authdialog =  this_.up('authdialog');
        if(check){// if user choose checkboxfield , user input value userid and password save to localstore
            local.set('usid',authdialog.down('textfield[name=userid]').getValue());
            local.set('pwd',authdialog.down('textfield[name=password]').getValue());
        }else{
            local.set('usid','');
            local.set('pwd','');
        }
    },
    /**
     * 用户中心使用，退出系统
     */
    onSignOut : function(btn,e,eOpts){
    	EUtil.showMsg({
			option:3,
			title : '退出系统',
			message : '您确认要退出系统？',
			callback : function(btn){
				if(btn === 'ok'){
					EUtil.RS({
						url: reqUrl.sys.logout,
						callback: function(result) {
							if(result.success){
								session.set('loginState',false);
								if(session.get('enableIM') === "true"){
									webSocketUtils.closeScoket();
								}
								window.location.href = window.location.origin + window.location.pathname || '';
							}else{
								EUtil.toast(result.msg_info,2);
							}
						}
					});
				}
			}
		});
    },
    /**
     * 用户中心使用，修改密码
     */
    onChenagePasscode : function(btn,e,eOpts){
    	Ext.create('Ext.window.Window',{
    		title : I18N.PasswordResetTitle,
    	 	modal:true,
    		iconCls : 'fa fa-edit',
    		width:400,
    		resizable:false,
    		closable:true,
    		layout:'fit',
    		items:[{
    			xtype: 'authdialog',
    			itemId : 'changePasscodeForm',
                autoComplete: false,
                bodyPadding: '20 20',
                layout: {
                    type: 'vbox',
                    align: 'stretch'
                },
                defaults : {
                    margin: '10 0'
                },
                items:[{
                    xtype: 'textfield',
                    cls: 'auth-textbox',
                    height: 55,
                    inputType: 'password',
                    name: 'sourcePwd',
                    hideLabel: true,
                    allowBlank: false,
                    emptyText: '请输入原登录密码',
                    triggers: {
                        glyphed: {
                        	cls: 'trigger-glyph-noop auth-password-trigger'
                        }
                    }
                },{
                    xtype: 'textfield',
                    cls: 'auth-textbox',
                    height: 55,
                    inputType: 'password',
                    name: 'newPassword',
                    hideLabel: true,
                    allowBlank: false,
                    emptyText: '请输入新密码',
                    triggers: {
                        glyphed: {
                        	cls: 'trigger-glyph-noop auth-password-trigger'
                        }
                    }
        		},{
                    xtype: 'textfield',
                    cls: 'auth-textbox',
                    height: 55,
                    inputType: 'password',
                    name: 'confimPwd',
                    hideLabel: true,
                    allowBlank: false,
                    emptyText: '请再次输入新密码',
                    triggers: {
                        glyphed: {
                        	cls: 'trigger-glyph-noop auth-password-trigger'
                        }
                    }
        		},{
                    xtype: 'button',
                    reference: 'resetPassword',
                    scale: 'large',
                    ui: 'soft-blue',
                    formBind: true,
                    text: I18N.Selected,
                    listeners: {
                        click: function(btn){
                        	var form = btn.up('#changePasscodeForm');
                        	EUtil.RS({
        						url: reqUrl.sys.changePassCode,
        						params:form.getValues(),
        						callback: function(result) {
        							if(result.success){
        								EUtil.showMsg({message:I18N.PasswordResetSuccess,option:0,
        									callback:function(btn){
        										session.set('loginState',false);
                								window.location.href = window.location.origin + window.location.pathname || '';
    										}
        								})
        							}else{
        								EUtil.toast(result.msg_info,2);
        							}
        						}
        					});
                        }
                    }
        		}]
    		}]
    	}).show();
    }
});
